RESTful and JSON remoting

Testing remoting

First install Advanced REST Client Chrome extension or use curl (for Unix-like systems) from the command line.

Using curl

If using curl, for commodity add a line containing -w "\n" to your ~/.curlrc file

It is also useful installing jq command-line JSON processor to pretty-print the JSON output.

Example scripts with curl:

Run the following scripts from the command line:

Creates (POST) a new Thin Crust pizza base:

curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name: "Thin Crust"}' http://localhost:8080/pizzashop/bases

Creates (POST) two new pizza bases (Cheesy Crust and Thick Crust):

curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '[{name: "Cheesy Crust"},{name: "Thick Crust"}]' http://localhost:8080/pizzashop/bases/jsonArray

Creates (POST) four new toppings: Fresh Tomato, Prawns, Mozarella (the z typo is deliberate) and Bogus:

curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '[{name: "Fresh Tomato"},{name: "Prawns"},{name: "Mozarella"},{name: "Bogus"}]' http://localhost:8080/pizzashop/toppings/jsonArray

Removes (DELETE) the Bogus (id=7) topping:

curl -i -X DELETE -H "Accept: application/json" http://localhost:8080/pizzashop/toppings/7

Retrieves (GET) the info about topping 6 (check self-generated version number):

curl -H "Accept: application/json" http://localhost:8080/pizzashop/toppings/6 | jq .

Updates (PUT) the name and version values for Mozzarella (id=6) topping (in JSON data, previous version number must be included; id=6 is not required here):

curl -i -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:"Mozzarella",version:1}' http://localhost:8080/pizzashop/topping/6

Retrieves (GET) the list of toppings:

curl -H "Accept: application/json" http://localhost:8080/pizzashop/toppings | jq .

Retrieves (GET) the info about topping 6:

curl -H "Accept: application/json" http://localhost:8080/pizzashop/toppings/6 | jq .

Creates (POST) an new Napolitana thin-crust pizza:

curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:"Napolitana",price:7.5,base:{id:1},toppings:[{name: "Anchovy fillets"},{name: "Mozzarella"}]}' http://localhost:8080/pizzashop/pizzas

Creates (POST) a new pizza order:

This does not work :-?

curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:"Stefan",total:7.5,address:"Sydney, AU",deliveryDate:1314595427866,id:{shopCountry:"AU",shopCity:"Sydney",shopName:"Pizza Pan 1"},pizzas:[{id:8,version:1}]}' http://localhost:8080/pizzashop/pizzaorders

Using a REST client

GET request: all toppings

GET request from REST client

POST request: create new Pizza (Achtung! POST is not safe - idempotence)

POST request from REST client